﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace Microsoft.Practices.ObjectBuilder2 {
    public static class ArrayHelper {
        public static T Find<T>(T[] array,
                Predicate<T> match) {

            foreach (var o in array) {
                if (match(o)) {
                    return o;
                }
            }

            return default(T);
        }
        public static T[] FindAll<T>(T[] array,
               Predicate<T> match) {

            List<T> list = new List<T>();
            foreach (var o in array) {
                if (match(o)) {
                    list.Add(o);
                }
            }

            return list.ToArray();  
        }

        //forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1846607&SiteID=1
        public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter) {
            if (array == null) {
                throw new ArgumentNullException("array");
            }

            if (converter == null) {
                throw new ArgumentNullException("converter");
            }

            TOutput[] tempArray = new TOutput[array.Length];

            for (int i = 0; i < array.Length; i++) {
                tempArray[i] = converter(array[i]);
            }

            return tempArray;

        }

          //Thanks to ctavareshttp://www.codeplex.com/unity/Thread/View.aspx?ThreadId=31275
          //Array.Find<T>(T[] array, Predicate<T> p) becomes:

          //  T result = (from item in array where i => p(i) select item).FirstOrDefault();

          //  Array.FindAll<T>(T[] array, Predicate<T> p) becomes:

          //  T[] result = (from item in array where i => p(i) select item).ToArray();

          //  Array.ConvertAll<TIn, TOut>(T[] array, Converter<TIn, TOut> convert) becomes:

          //  TOut[] result = (from item in array select item => convert(item)).ToArray();

          //  List<T>.RemoveAll doesn't have a Linq equivalent, because it mutates the underlying list rather than producing a new sequence. But List<T>.RemoveAll is supported on Silverlight, so you don't need to replace it.
    }
}
